//
//  STLoginController.m
//  CuiBoStore
//
//  Created by 崔博 on 16/12/1.
//  Copyright © 2016年 崔博. All rights reserved.
//

#import "STLoginController.h"
#import "STLoginView.h"
#import "STInputView.h"
#import <UMSocialCore/UMSocialCore.h>
#import "STLgoinModel.h"
#import "STNextViewController.h"
#import "STRegisHanlde.h"

@interface STLoginController ()
@property (nonatomic, strong) STLoginView *threeLoginView;
@property (nonatomic, strong) STInputView *inputView;
@property (nonatomic, assign) VCStyle style;
@end


@implementation STLoginController
- (instancetype)initWithVCStyle:(VCStyle)style
{
    if (self = [super init]) {
        self.style = style;
    }
    return self;
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = BACKGROUND_COLOR;
    [self addSubviews];
    [self configVC];
}

#pragma mark - methoed
// 添加布局子控件
- (void)addSubviews{
    [self.view addSubview:self.inputView];
    [_inputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.left.equalTo(self.view);
    }];
    [self.view layoutIfNeeded];
    self.threeLoginView.frame = CGRectMake(0, _inputView.maxY + 10, SCREEN_WIDTH, 200);
    [self.view addSubview:self.threeLoginView];
}
// 登录
- (void)login:(NSDictionary *)userInfo{
    [STRegisHanlde executeLogin:userInfo sucess:^(id obj) {
        
        [[NSUserDefaults standardUserDefaults] setObject:isLogin forKey:@"1"];
        [[NSUserDefaults standardUserDefaults] setValue:obj[@"MemberId"] forKey:KMemberID];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [self.view makeToast:obj[@"ErrorMessage"]];
        [self.navigationController popToRootViewControllerAnimated:YES];
    } failed:^(id obj) {
        
    }];
}
// 配置控制器
- (void)configVC
{
    if (self.style == LoginStyle) {
        self.inputView.btn_Title = @"登录";
        self.inputView.isHiddedLogin = YES;
    }
}
#pragma mark - lazy
- (STLoginView *)threeLoginView
{
    if (!_threeLoginView) {
        _threeLoginView = [STLoginView loginView];
        _threeLoginView.block = ^{
            [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_QQ currentViewController:nil completion:^(id result, NSError *error) {
                if (error) {
                    
                } else {
                    UMSocialUserInfoResponse *resp = result;
                    // 授权信息
                    NSLog(@"QQ uid: %@", resp.uid);
                    NSLog(@"QQ openid: %@", resp.openid);
                    NSLog(@"QQ accessToken: %@", resp.accessToken);
                    NSLog(@"QQ expiration: %@", resp.expiration);
                    
                    // 用户信息
                    NSLog(@"QQ name: %@", resp.name);
                    NSLog(@"QQ iconurl: %@", resp.iconurl);
                    NSLog(@"QQ gender: %@", resp.gender);
                    
                    // 第三方平台SDK源数据
                    NSLog(@"QQ originalResponse: %@", resp.originalResponse);
                }
            }];
        };
    }
    return _threeLoginView;
}
- (UIView *)inputView
{
    @weakify(self);
    if (!_inputView) {
        _inputView = [[STInputView alloc] init];
        _inputView.block = ^(UIButton *btn,STLgoinModel *model){
            if (self.style == LoginStyle) {
                NSDictionary *dic = @{@"LoginName":model.userName,
                                      @"Lpassword":model.pwd};
                [weak_self login:dic];
            }else
            {
               STNextViewController *next = [[STNextViewController alloc] init];
                next.loginModel = model;
                [weak_self.navigationController pushViewController:next animated:YES];
            }
        };
    }
    return _inputView;
}
@end
